//
//  LMButton.m
//  easybook
//
//  Created by longmin on 16/6/29.
//  Copyright © 2016年 longmin. All rights reserved.
//

#import "LMButton.h"
#import "UIImage+Color.h"

@implementation LMButton

#pragma mark - 生命周期

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        self.titleLabel.font = _kFontMain;
    }
    return self;
}
#pragma mark - set方法和get方法
#pragma mark - IBActions
-(void)btnClick:(LMButton *)btn{
    
    if (self.onClick) {
        self.onClick(btn);
    }
}
#pragma mark - Public

+(LMButton *)lm_buttonWithType:(LMButtonType)type title:(NSString *)title {
    
    LMButton *btn = [[self alloc] init];
    
    btn.customType = type;
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UIImage *normalImg = [UIImage lm_imageWithColor:kColor4B79AF];
    [btn setBackgroundImage:normalImg forState:UIControlStateNormal];
    
    UIImage *highlightImg = [UIImage lm_imageWithColor:kRGBCOLOR(52, 92, 138)];
    [btn setBackgroundImage:highlightImg forState:UIControlStateHighlighted];
    
    UIImage *canNotUseImg = [UIImage lm_imageWithColor:kRGBACOLOR(75, 121, 175, 0.5)];
    [btn setBackgroundImage:canNotUseImg forState:UIControlStateDisabled];
    
    [btn renderWithType:type];
    
    return btn;
}

#pragma mark - Private

-(void)renderWithType:(LMButtonType)type {
    
    if (type == LMButtonTypeBottom) {
        //底部固定按钮
        self.width = __kScreenWidth;
        self.height = 50;
        self.titleLabel.font = _kFontNav;
        return;
    }
    
    self.titleLabel.font = _kFont32;
    self.layer.cornerRadius = 1.5;
    self.clipsToBounds = YES;
    
    if (type == LMButtonTypeCancel) {
        
        self.height = 44;
        self.width = 105 * __kwRatio;
        [self setBackgroundImage:[UIImage lm_imageWithColor:kColorD9D9D9] forState:UIControlStateNormal];
        
        [self setBackgroundImage:[UIImage lm_imageWithColor:kRGBCOLOR(203, 203, 203)] forState:UIControlStateHighlighted];
        
    }else if (type == LMButtonTypeSmallWhite) {
        
        self.width = 175 * __kwRatio;
        self.height = 44;
        
        [self setWhiteBGBorderLine];
        
    }else if (type == LMButtonTypeMove || type == LMButtonTypeBorderLine) {
        //移动按钮
        self.width = __kScreenWidth - MK_PADDING_30 * 2;
        self.height = 44;

        if (type == LMButtonTypeBorderLine) {
            [self setWhiteBGBorderLine];
        }
        
    }else if (type == LMButtonTypeSmallBlue) {
        //蓝色小按钮
        self.height = 44;
        self.width = 175 * __kwRatio;

    }else if (type == LMButtonTypeNone){
        //无背景色边款按钮
    }
    
}


/**
 设置白底蓝边
 */
-(void)setWhiteBGBorderLine {
    
    [self setTitleColor:kColor4B79AF forState:UIControlStateNormal];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    
    [self setBackgroundImage:[UIImage lm_imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage lm_imageWithColor:kColor4B79AF] forState:UIControlStateHighlighted];
    [self setBackgroundImage:[UIImage lm_imageWithColor:KColorF0F0F0] forState:UIControlStateDisabled];
    self.layer.borderColor = kColor4B79AF.CGColor;
    self.layer.borderWidth = 1.0;
}

#pragma mark - 数据源和代理方法
#pragma mark - UIButton -> 下面放继承自UIButton的方法

-(void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    
    
    if (self.customType != LMButtonTypeSmallWhite) {
        return;
    }
    
    if (enabled) {
        
        self.layer.borderColor = kColor4B79AF.CGColor;
        
    }else {
        
        self.layer.borderColor = kColorD9D9D9.CGColor;
        
    }
    
}



@end
